package com.superb.allocation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.superb.allocation.api.dto.DictTree;
import com.superb.common.database.mybatis.service.impl.SuperbServiceImpl;
import org.springframework.stereotype.Service;
import com.superb.allocation.api.entity.AllocationDictType;
import com.superb.allocation.mapper.AllocationDictTypeMapper;
import com.superb.allocation.service.AllocationDictTypeService;

import java.util.List;

/**
 * 数据字典类型;(allocation_dict_type)表服务实现类
 * @Author: ajie
 * @CreateTime: 2024-6-27
 */
@Service
public class AllocationDictTypeServiceImpl extends SuperbServiceImpl<AllocationDictTypeMapper, AllocationDictType> implements AllocationDictTypeService {
    @Override
    public List<AllocationDictType> getChildren(List<AllocationDictType> list) {
        for (AllocationDictType dictType : list) {
            LambdaQueryWrapper<AllocationDictType> lqw = new LambdaQueryWrapper<>();
            lqw.eq(AllocationDictType::getParentId, dictType.getId());
            lqw.orderByAsc(AllocationDictType::getSort);
            List<AllocationDictType> dictTypes = super.list(lqw);
            if (dictTypes.size() > 0) {
                dictType.setChildren(dictTypes);
                getChildren(dictTypes);
            }
        }
        return list;
    }

    @Override
    public List<DictTree> levelDict(String parentId) {
        List<DictTree> list = baseMapper.treeTranslate(parentId);
        return buildTree(list);
    }

    private List<DictTree> buildTree(List<DictTree> list) {
        for (DictTree tree : list) {
            List<DictTree> trees = baseMapper.treeTranslate(tree.getId());
            if (trees.size() > 0) {
                tree.setChildren(trees);
                buildTree(trees);
            }
        }
        return list;
    }
}